package ybt.vueelement.controller;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import ybt.vueelement.custom.AjaxResult;
import ybt.vueelement.custom.TablePageable;
import ybt.vueelement.entity.Course;
import ybt.vueelement.entity.Grade;
import ybt.vueelement.entity.Student;
import ybt.vueelement.entity.SysUser;
import ybt.vueelement.entity.TbClass;
import ybt.vueelement.form.GradeForm;
import ybt.vueelement.security.UserUtils;
import ybt.vueelement.service.CourseService;
import ybt.vueelement.service.GradeService;
import ybt.vueelement.service.StudentService;
import ybt.vueelement.service.TbClassService;

@Controller
@RequestMapping(value = "/grade")
public class GradeController {
	@Autowired
	private GradeService gradeService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private TbClassService tbclassService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private StudentService studentService;
	
	@RequestMapping(value = "/peasonGrade")
	public Object peasonGrade(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		List<Grade> list = gradeService.findByStudentId(id);
	
		return new AjaxResult(list);
	}

//	<***************************************************************************************>	
//  班级成绩录入
	@RequestMapping(value = "/bjcjlr")
	public Object bjcjlr(ModelMap modelMap) {
		List<TbClass> tbClasses = tbclassService.findAll();
		List<Course> courses = courseService.findAll();
		modelMap.put("bjs", tbClasses);
		modelMap.put("kcs", courses);
		return new AjaxResult(tbClasses);
		
	}

//显示表
	@RequestMapping(value = "/bjcj")
	@ResponseBody
	public Object bjcj(GradeForm form,Integer bjId, Integer kcId, ModelMap modelMap) {
		List<Student> students = studentService.findByTbClassId(bjId);
		System.out.println(kcId);
		List<Grade> grades = new ArrayList<Grade>();
		for (Student s : students) {
			Grade grade = gradeService.findByStudentIdAndCourseId(s.getId(), kcId);
			if (grade == null) {
				Grade grade1 = new Grade();
				grade1.setCourse(courseService.findById(kcId));
				
				grade1.setStudent(studentService.findById(s.getId()));
				gradeService.save(grade1);
				grades.add(grade1);
			} else {
				grades.add(grade);
			}
		}
		System.out.println(grades);
		return new AjaxResult(grades);

	}

//修改成绩
	@RequestMapping(value = "/cjChange")
	@ResponseBody
	public void cjChange(Integer id, Integer grade) {
		Grade g = gradeService.findById(id);
		g.setGrade(grade);
		gradeService.save(g);

	}
//<***************************************************************************************>	
	

	//                            个人成绩录入
	@RequestMapping(value = "/grcjgl")
	public String personGradelr(ModelMap modelMap) {
		List<TbClass> tbClasses=tbclassService.findAll();

		modelMap.put("list", tbClasses);
		return "grade/grcjgl";
	}
	//从班级获取学生名单
	@RequestMapping(value = "/getStu")
	@ResponseBody
	public Object getStu(Integer bjId,ModelMap modelMap) {
		List<Student> students=studentService.findByTbClassId(bjId);
		return new AjaxResult(students);
	}
	//得到学生成绩
	@RequestMapping(value = "/getPersonGrade")
	@ResponseBody
	public Object getPersonGrade(Integer stuId,ModelMap modelMap) {
		List<Grade> grades=gradeService.findByStudentId(stuId);
		modelMap.put("list", grades);
		return new AjaxResult(grades);
	}
	//修改成绩
	@RequestMapping(value = "/cjChange1")
	@ResponseBody
	public void cjChange1(Integer id,Integer grade) {
		Grade grade2=gradeService.findById(id);
		grade2.setGrade(grade);
		gradeService.save(grade2);
	}
//	<***************************************************************************************>	
	
	//           班级成绩查询
	
	@RequestMapping(value = "/cjcx")
	public String bjcjcx(ModelMap modelMap) {
		List<TbClass> list=tbclassService.findAll();
		modelMap.put("list", list);
		return "grade/cjcx";
	}
	
	//成绩表
	@RequestMapping(value = "/bjChange")
	@ResponseBody
	public Object bjChange(Integer bjId,ModelMap modelMap) {
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		System.out.println(bjId);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		System.out.println(cjb);
		return cjb;
	}
	@RequestMapping(value = "/bjChange2")
	@ResponseBody
	public Object bjChange2(Integer bjId,ModelMap modelMap) {
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		System.out.println(bjId);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		System.out.println(cjb);
		return courses;
	}
	//	<***************************************************************************************>
	//学生个人成绩查询
	@RequestMapping(value = "/grcjcx")
	@ResponseBody
	public Object grcjcx(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		List<Grade> list=gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return list;
	}
	@RequestMapping(value = "/grcjcx1")
	@ResponseBody
	public Object getPersonGrade1(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer stuId=user.getId();
		List<Grade> grades=gradeService.findByStudentId(stuId);
		modelMap.put("list", grades);
		return new AjaxResult(grades);
	}
	//学生所在班级成绩查询
	@RequestMapping(value = "/grbjcjcx1")
	@ResponseBody
	public Object grbjcjcx(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		Student student=studentService.findById(id);
		Integer bjId=student.getTbClass().getId();
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		modelMap.put("stu", student);
		return new AjaxResult(cjb);
	}
	/*11111*/
	@RequestMapping(value = "/grbjcjcxcourse")
	@ResponseBody
	public Object bjChange3(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		Student student=studentService.findById(id);
		Integer bjId=student.getTbClass().getId();
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		System.out.println(bjId);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		System.out.println(cjb);
		return courses;
	}
	@RequestMapping(value = "/grbjcjcxcjb")
	@ResponseBody
	public Object bjChange4(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		Student student=studentService.findById(id);
		Integer bjId=student.getTbClass().getId();
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		System.out.println(bjId);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		System.out.println(cjb);
		System.out.println(courses);
		return cjb;
	}
//	<***************************************************************************************>	
	@SuppressWarnings("serial")
	public Specification<Grade> buildSpec(Integer bjId,Integer kcId) {
		Specification<Grade> spec=new Specification<Grade>() {

			@Override
			public Predicate toPredicate(Root<Grade> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(bjId!=null) {
					rules.add(cb.equal(root.get("student").get("tbClass").get("id").as(String.class), bjId));
				}
				if(kcId!=null) {
					rules.add(cb.equal(root.get("course").get("id").as(String.class), kcId));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	@RequestMapping(value = "/page")
	@ResponseBody
	public Object page(TablePageable tablePaegable,Integer bjId,Integer kcId) {
		List<Student> students=studentService.findByTbClassId(bjId);
		for(Student s:students) {
		Grade grade=gradeService.findByStudentIdAndCourseId(s.getId(), kcId);
		if(grade==null) {
			Grade grade1=new Grade();
			grade1.setCourse(courseService.findById(kcId));
			grade1.setStudent(studentService.findById(s.getId()));
			grade1.setGrade(null);
			gradeService.save(grade1);
			}
		}
		PageRequest pageable = tablePaegable.buildPageRequest();
		Specification<Grade> spec = buildSpec(bjId,kcId);
		Page<Grade> page = gradeService.findAll(spec, pageable);
		return AjaxResult.buildTableResult(page);
	}

	@RequestMapping(value = "/page1")
	@ResponseBody
	public Object page1(TablePageable tablePaegable,Integer bjId,Integer stuId) {
		List<Student> students=studentService.findByTbClassId(bjId);
		List<Grade> grades=gradeService.findByStudentId(stuId);
		PageRequest pageable = tablePaegable.buildPageRequest();
		Specification<Grade> spec = buildSpec(bjId,stuId);
		Page<Grade> page = gradeService.findAll(spec, pageable);
		return AjaxResult.buildTableResult(page);
	}
	
}
